package com.java.collect.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.java.clients.ProductClient;
import com.java.collect.mapper.CollectMapper;
import com.java.collect.service.CollectService;
import com.java.param.ProductCollectParam;
import com.java.param.ProductIdRemoveParam;
import com.java.pojo.Collect;
import com.java.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class CollectServiceImpl implements CollectService {

    @Autowired
    private CollectMapper collectMapper;
    @Autowired
    private ProductClient productClient;
    /**
     * 根据用户id和商品id，保存到收藏中
     *
     * @param collect
     * @return
     */
    @Override
    public R save(Collect collect) {
        //分解参数
        Integer userId = collect.getUserId();
        Integer productId = collect.getProductId();
        //封装参数，进行判断该商品是否已经加入收藏
        QueryWrapper<Collect> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        queryWrapper.eq("product_id",productId);
        Long count = collectMapper.selectCount(queryWrapper);

        if(count > 0){
            log.info("CollectServiceImpl.save 业务已经完成,结果:{}", count);
            return R.fail("该商品已在收藏夹，无需再次添加！");
        }
        collect.setCollectTime(System.currentTimeMillis());
        int rows = collectMapper.insert(collect);
        if(rows==0){
            log.info("CollectServiceImpl.save 业务已经完成,结果:{}", "收藏失败");
            return R.fail("收藏失败！");
        }
        log.info("CollectServiceImpl.save 业务已经完成,结果:{}", "收藏成功");
        return R.ok("收藏成功！");
    }

    /**
     * 根据用户id和商品id删除收藏商品
     *
     * @param collect
     * @return
     */
    @Override
    public R remove(Collect collect) {
        QueryWrapper<Collect> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",collect.getUserId());
        queryWrapper.eq("product_id",collect.getProductId());
        int rows = collectMapper.delete(queryWrapper);
        log.info("CollectServiceImpl.remove 业务已经完成,结果:{}", rows);
        return R.ok("收藏移除成功！");
    }

    /**
     * 根据userid查询全部收藏商品
     * @return
     */
    @Override
    public R list(Integer userId) {

        //根据用户id查询收藏商品id集合
        QueryWrapper<Collect> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        queryWrapper.select("product_id");
        List<Collect> productIdsList = collectMapper.selectList(queryWrapper);
        List<Object> list = collectMapper.selectObjs(queryWrapper);

        //封装商品id
        List<Integer> ids = new ArrayList<>();
        for (Object id : list) {
            ids.add((Integer) id);
        }
        ProductCollectParam productCollectParam = new ProductCollectParam();
        productCollectParam.setProductIds(ids);

        //通过feign客户端调用product服务查询商品集合
        R r = productClient.productIds(productCollectParam);
        log.info("CollectServiceImpl.list 业务已经完成,结果:{}", r);
        return r;
    }

    /**
     * 删除收藏下所有该商品
     *
     * @param productIdRemoveParam
     * @return
     */
    @Override
    public R productRemove(ProductIdRemoveParam productIdRemoveParam) {
        QueryWrapper<Collect> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("product_id",productIdRemoveParam.getProductId());
        int rows = collectMapper.delete(queryWrapper);
        log.info("CollectServiceImpl.productRemove 业务已经完成,结果:{}", rows);
        if(rows==0){
            return R.fail("收藏夹商品删除失败！");
        }
        return R.ok("收藏夹商品删除成功！");
    }
}
